<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

namespace Lg\Http;

use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Lg\Support\Str;

/**
 * Description of Request
 *
 * @author Allen <allen@lg4.cn>
 */
class Request extends SymfonyRequest {

    public static $instance;

    /**
     * 创建默认Request
     *
     * @return static Request
     */
    public static function classic() {
        if (is_null(static::$instance)) {
            static::enableHttpMethodParameterOverride();
            static::$instance = static::createFromGlobals();
        }
        return static::$instance;
    }

    /**
     * 是否使用Ajax
     * @return boolean
     */
    public function isAjax() {
        return $this->isXmlHttpRequest();
    }

    /**
     * 是否使用SSL
     * @return boolean 
     */
    public function isSSL() {
        return $this->isSecure();
    }

    /**
     * 获取IP地址
     * @return string
     */
    public function ip() {
        return $this->getClientIp();
    }

    public function ips() {
        return $this->getClientIps();
    }

    public function input($key = null, $default = null) {
        $input = $this->getInputSource()->all() + $this->query->all();
        return data_get($input, $key, $default);
    }

    protected function retrieveItem($source, $key, $default) {
        if (is_null($key)) {
            return $this->$source->all();
        }
        return $this->$source->get($key, $default);
    }

    /**
     * Get the input source for the request.
     *
     * @return \Symfony\Component\HttpFoundation\ParameterBag
     */
    protected function getInputSource() {
        if ($this->isJson()) {
            return $this->json();
        }
        return $this->getRealMethod() == 'GET' ? $this->query : $this->request;
    }

    /**
     * Determine if the request is sending JSON.
     *
     * @return bool
     */
    public function isJson() {
        return Str::contains($this->header('CONTENT_TYPE'), ['/json', '+json']);
    }

    public function session() {
        if (!$this->hasSession()) {
            $this->setSession(\Lg\Session\Session::instance());
        }
        return $this->getSession();
    }

    public function file($key = null, $default = null) {
        return data_get($this->files->all(), $key, $default);
    }

    /**
     * Retrieve a query string item from the request.
     *
     * @param  string  $key
     * @param  string|array|null  $default
     * @return string|array
     */
    public function query($key = null, $default = null) {
        return $this->retrieveItem('query', $key, $default);
    }

    public function hasHeader($key) {
        return !is_null($this->header($key));
    }

    public function header($key = null, $default = null) {
        return $this->retrieveItem('headers', $key, $default);
    }

    public function server($key = null, $default = null) {
        return $this->retrieveItem('server', $key, $default);
    }

    public function hasCookie($key) {
        return !is_null($this->cookie($key));
    }

    public function cookie($key = null, $default = null) {
        return $this->retrieveItem('cookies', $key, $default);
    }

    public function method() {
        return $this->getMethod();
    }

    /**
     * Get the root URL for the application.
     *
     * @return string
     */
    public function root() {
        return rtrim($this->getSchemeAndHttpHost() . $this->getBaseUrl(), '/');
    }

    /**
     * Get the URL (no query string) for the request.
     *
     * @return string
     */
    public function url() {
        return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/');
    }

    public static function __callStatic($name, $arguments) {
        return call_user_func_array([static::classic(), $name], $arguments);
    }

}
